<template>
  <div>
    <div class="titles" :class="type" ref="titles">
      <div
        @click="click($event, index)"
        v-for="(item, index) in titles"
        :key="index"
        :class="{ selected: tempSelectIndex === index }"
      >
        {{ item }}
      </div>
      <div ref="slider" class="slider"></div>
    </div>
    <div class="content">
      <slot></slot>
    </div>
  </div>
</template>

<script>
export default {
  name: "mu-tabs",
  data() {
    return {
      tempSelectIndex: this.selectIndex, //选中的下标
      titles: [],
    };
  },
  props: {
    selectIndex: {
      type: Number,
      default: 0,
    },
    type: {
      type: String,
      default: "base",
    },
  },
  mounted() {
    for (let key in this.$slots.default) {
      let item = this.$slots.default[key];
      this.titles.push(item.componentOptions.propsData.label);
    }
    this.$nextTick(() => {
      const children = this.$refs.titles.children;
      if (!children.length) {
        return;
      }
      const item = children[this.tempSelectIndex];
      this.calc(item, this.tempSelectIndex);
    });
  },
  watch: {
    selectIndex(value) {
      this.tempSelectIndex = value;
    },
  },
  methods: {
    //滑动
    click(e, index) {
      const dom = e.target || e.currentTarget;
      this.calc(dom, index);
    },
    //计算
    calc(dom, index) {
      this.tempSelectIndex = index;
      for (let key in this.$slots.default) {
        let item = this.$slots.default[key];
        if (this.tempSelectIndex != key) {
          item.elm.style.display = "none";
        } else {
          item.elm.style.display = "";
        }
      }
      if (this.type === "base") {
        let width = dom.clientWidth - 40;
        this.$refs.slider.style.left = dom.offsetLeft + 20 + "px";
        this.$refs.slider.style.width = width + "px";
      }
    },
  },
};
</script>

<style scoped lang="scss">
@import "../variable.scss";

.content {
  margin-top: 10px;
}

.titles {
  font-size: 14px;
  cursor: pointer;
  position: relative;

  .selected {
    color: $blue;
  }

  .slider {
    display: none;
    padding: 0;
    margin: 0;
  }

  div {
    display: inline-block;
    padding: 10px 20px;

    &:hover {
      color: $blue;
    }
  }

  &.base {
    border-bottom: 2px solid $border-color;
    margin-bottom: 10px;

    .slider {
      background: $blue;
      display: inline-block;
      width: 70px;
      height: 2px;
      box-sizing: border-box;
      padding: 0;
      position: absolute;
      left: 0;
      bottom: -2px;
      transition: left 0.3s ease-in-out, width 0.3s ease-in-out;
    }
  }

  &.card {
    border-bottom: 1px solid $border-color;

    div {
      position: relative;
      z-index: 2;
      bottom: -1px;
      border-right: 1px solid $border-color;
      border-top: 1px solid $border-color;

      &:first-child {
        border-left: 1px solid $border-color;
        border-top-left-radius: 3px;
      }

      &:nth-last-child(2) {
        border-top-right-radius: 3px;
      }
    }

    .selected {
      border-bottom: 1px solid white;
    }
  }
}
</style>